package com.ruoyi.web.controller.system.city;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.system.domain.city.CityTradeSecond;
import com.ruoyi.system.domain.city.vo.TradeOptionVo;
import com.ruoyi.system.domain.city.vo.TradeSecondOptionVo;
import com.ruoyi.system.service.city.ICityTradeSecondService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.city.CityTrade;
import com.ruoyi.system.service.city.ICityTradeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 一级行业Controller
 *
 * @author cjq
 * @date 2023-07-24
 */
@RestController
@RequestMapping("/system/city/trade")
public class CityTradeController extends BaseController {
    @Autowired
    private ICityTradeService cityTradeService;

    @Autowired
    private ICityTradeSecondService cityTradeSecondService;

    /**
     * 查询一级行业列表
     */
    @PreAuthorize("@ss.hasPermi('city:trade:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityTrade cityTrade) {
        startPage();
        List<CityTrade> list = cityTradeService.selectCityTradeList(cityTrade);
        return getDataTable(list);
    }

    /**
     * 查询一级行业列表
     */
    @GetMapping("/tradeList")
    public List<CityTrade> tradeList(CityTrade cityTrade) {
        startPage();
        List<CityTrade> list = cityTradeService.selectCityTradeList(cityTrade);
        return list;
    }

    /**
     * 查询行业级联选择
     */
    @GetMapping("/tradeOptions")
    public List<TradeOptionVo> tradeOptions(CityTrade cityTrade) {
        startPage();
        List<CityTrade> list = cityTradeService.selectCityTradeList(cityTrade);
        List<TradeOptionVo> tradeOptionVoList = new ArrayList<>();
        if (!list.isEmpty()) {
            for (CityTrade c : list) {
                TradeOptionVo tradeOptionVo = new TradeOptionVo();
                tradeOptionVo.setValue(c.getTradeName());
                tradeOptionVo.setLabel(c.getTradeName());
                CityTradeSecond cityTradeSecond = new CityTradeSecond();
                cityTradeSecond.setTradeId(c.getTradeId());
                List<CityTradeSecond> cityTradeSecondList = cityTradeSecondService.selectCityTradeSecondList(cityTradeSecond);
                List<TradeSecondOptionVo> tradeSecondOptionVoList = new ArrayList<>();
                if (!cityTradeSecondList.isEmpty()) {
                    for (CityTradeSecond s : cityTradeSecondList) {
                        TradeSecondOptionVo tradeSecondOptionVo = new TradeSecondOptionVo();
                        tradeSecondOptionVo.setValue(s.getTradeSecondName());
                        tradeSecondOptionVo.setLabel(s.getTradeSecondName());
                        tradeSecondOptionVoList.add(tradeSecondOptionVo);
                    }
                }
                tradeOptionVo.setChildren(tradeSecondOptionVoList);
                tradeOptionVoList.add(tradeOptionVo);
            }
        }
        return tradeOptionVoList;
    }

    /**
     * 导出一级行业列表
     */
    @PreAuthorize("@ss.hasPermi('city:trade:export')")
    @Log(title = "一级行业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityTrade cityTrade) {
        List<CityTrade> list = cityTradeService.selectCityTradeList(cityTrade);
        ExcelUtil<CityTrade> util = new ExcelUtil<CityTrade>(CityTrade.class);
        util.exportExcel(response, list, "一级行业数据");
    }

    /**
     * 获取一级行业详细信息
     */
    @PreAuthorize("@ss.hasPermi('city:trade:query')")
    @GetMapping(value = "/{tradeId}")
    public AjaxResult getInfo(@PathVariable("tradeId") Long tradeId) {
        return success(cityTradeService.selectCityTradeByTradeId(tradeId));
    }

    /**
     * 新增一级行业
     */
    @PreAuthorize("@ss.hasPermi('city:trade:add')")
    @Log(title = "一级行业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityTrade cityTrade) {
        cityTrade.setTradeId(IdUtil.getSnowflakeNextId());
        return toAjax(cityTradeService.insertCityTrade(cityTrade));
    }

    /**
     * 修改一级行业
     */
    @PreAuthorize("@ss.hasPermi('city:trade:edit')")
    @Log(title = "一级行业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityTrade cityTrade) {
        return toAjax(cityTradeService.updateCityTrade(cityTrade));
    }

    /**
     * 删除一级行业
     */
    @PreAuthorize("@ss.hasPermi('city:trade:remove')")
    @Log(title = "一级行业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tradeIds}")
    public AjaxResult remove(@PathVariable Long[] tradeIds) {
        return toAjax(cityTradeService.deleteCityTradeByTradeIds(tradeIds));
    }
}
